package com.bidepan.spring6.resourceloader;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.Resource;

import java.io.IOException;

public class ResourceLoaderDemo {
    @Test
    public void demo1(){
        ApplicationContext context = new ClassPathXmlApplicationContext();
        Resource resource =  context.getResource("demo.txt");
        System.out.println(resource.getClass());
        System.out.println(resource.getFilename());
        System.out.println(resource.getDescription());
    }

    @Test
    public void demo2(){
        ApplicationContext context = new FileSystemXmlApplicationContext();
        Resource resource =  context.getResource("myTxt.txt");
        System.out.println(resource.getClass());
        System.out.println(resource.getFilename());
        System.out.println(resource.getDescription());
    }

    @Test
    public void demo3() throws IOException {
        ApplicationContext context = new FileSystemXmlApplicationContext();
        //通过文件前缀https:// 来强制指定使用哪个Resource实现类
        Resource resource =  context.getResource("https://www.baidu.com");
        System.out.println(resource.getClass());
        System.out.println(resource.getURI());
        System.out.println(resource.getDescription());
    }
}
